import cv2
import os
from tkinter import filedialog

# 获取视频路径
folder = filedialog.askdirectory()
os.chdir(folder)
a = 0  # 统计所有图片的进度
count = 0  # 用来统计视频的进度
catalog = os.listdir(folder)
all_c = len(catalog)   #获取总文件数

os.mkdir('cut')


for each in catalog:
    print(each+"开始啦========")
    video = cv2.VideoCapture(each)
    count += 1  #文件计数
    c = 1
    if video.isOpened():
        retval, frame = video.read()
    timeF =  30  #填写截取视频帧数
    while retval:
        retval,frame = video.read()
        if(c % timeF == 0):
            name = each.split(".")[0]
            path1 = folder + "/cut/" + name + "-" + str(c//timeF).zfill(5) + ".jpg"
            try:
                cv2.imwrite(path1,frame)
            except cv2.error:
                print("我走了下一位")
            a = a + 1
            print("第%d，共%d个，总照片数%d,当前%d张图片" % (count,all_c,a, c//timeF))
        c = c + 1
    cv2.waitKey(1)
    video.release()
    print("========="+each+"结束啦========")
print(f"全部结束了，总共有{a}张照片")